package controller

import (
	"admin/contrib/session"
	"admin/model"
	"common/helper"
	"encoding/json"
	"github.com/valyala/fasthttp"
	"time"
)

type RechargeController struct{}

// 订单列表
func (that *RechargeController) ChannelList(ctx *fasthttp.RequestCtx) {
	admin := session.AdminGet(ctx)
	if admin["loginuser"] != "admin" {
		helper.RetData(ctx, false, "权限不足")
		return
	}
	data, err := model.PayFactoryList(1)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	helper.RetData(ctx, true, data)
}

// History 存款历史记录
func (that *RechargeController) OrderList(ctx *fasthttp.RequestCtx) {

	param := model.DepositHistoryParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	param.ExportExcel = ctx.QueryArgs().GetUintOrZero("export_excel")
	if param.ExportExcel == 1 {
		param.Page = 1
		param.PageSize = model.ExportExcelPageSize
	}

	if param.Page < 1 {
		param.Page = 1
	}

	data, err := model.DepositList(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	if param.ExportExcel == 1 {
		_, exportData := model.RechargeListOpt(data, param.ExportExcel)
		exportTitle := []string{
			"订单ID",
			"玩家ID",
			"金额",
			"状态",
			"充值时间",
		}
		filename := "充值记录-" + time.Now().Format("20060102150405")
		if model.ExportExcel(filename, exportTitle, exportData) == nil {
			helper.RetData(ctx, true, filename)
		} else {
			helper.RetData(ctx, false, helper.RequestFail)
		}

		return
	} else {
		helper.RetData(ctx, true, data)
		return
	}
}

type FinishOrderParam struct {
	Id     string `json:"id"`
	State  int    `json:"state"`
	Hidden int    `json:"hidden"`
}

// 补单操作
// {"id":"62830512814276611","state":365}:
func (that *RechargeController) UpdateFinishOrder(ctx *fasthttp.RequestCtx) {
	s := model.GameConfigByType(4)
	if s.CfgValue == "0" || s.CfgValue == "" {
		helper.RetData(ctx, false, helper.Failure)
	} else {
		param := FinishOrderParam{}
		err := json.Unmarshal(ctx.PostBody(), &param)
		if err != nil {
			helper.RetData(ctx, false, err.Error())
			return
		}
		model.InsertLog("玩家订单补单操作", ctx)
		admin := session.AdminGet(ctx)
		if admin["loginuser"] != "admin" {
			helper.RetData(ctx, false, "权限不足")
			return
		}
		err = model.UpdateOrderFinish(param.Id, param.State, ctx)
		if err != nil {
			helper.RetData(ctx, false, err.Error())
			return
		}

		helper.RetData(ctx, true, "成功")
	}
	//helper.RetData(ctx, false, helper.Failure)
}

func (that *RechargeController) UpdateHiddenOrder(ctx *fasthttp.RequestCtx) {
	param := FinishOrderParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	model.InsertLog("玩家订单显示操作", ctx)
	//admin := session.AdminGet(ctx)
	//if admin["loginuser"] != "admin" {
	//	helper.RetData(ctx, false, "权限不足")
	//	return
	//}
	err = model.UpdateHiddenOrder(param.Id, param.Hidden, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}
